import Vue from 'vue'
import App from './App.vue'
import moment from 'moment'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';//样式文件一定要引入
import { getDicts } from "@/api/dict/data";
import { getToken} from '@/utils/auth'
import { parseTime, resetForm, addDateRange, selectDictLabel, download, handleTree } from "@/utils/ruoyi";
import BMap from 'bmap'
//导入路由
import router from './router';


// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.config.productionTip = false
Vue.prototype.$moment=moment

Vue.use(BMap)
Vue.use(ElementUI);
Vue.use(router);

Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.download = download
Vue.prototype.handleTree = handleTree

Vue.prototype.msgSuccess = function (msg) {
  this.$message({ showClose: true, message: msg, type: "success" });
}

Vue.prototype.msgError = function (msg) {
  this.$message({ showClose: true, message: msg, type: "error" });
}

Vue.prototype.msgInfo = function (msg) {
  this.$message.info(msg);
}

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

//路由守卫，当路由设置为guard为true时表示需要登录，验证token是否有值，未登录则跳转到登录页面
router.beforeEach((to,from,next)=> {
  //修改页面标题
  window.document.title = to.meta.title == undefined ? '华中版权交易平台' : to.meta.title
  //判断是否需要登录，需要登录的页面没有检测到token则拦截到登录页
  let isLogin = getToken();
  if(to.meta.guard){  //判断是否为true
    if(isLogin){  //判断是否为true
      next()
    }else{
      next('/login')//不为true跳转到登录页面
    }
  }else{
    next()//不为true往下执行
  }
})


